<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use App\Models\Category;
use App\Models\Type;
use Intervention\Image\ImageManagerStatic as Img;

class Product extends Model
{
    //
    protected $table = 'product';

    protected $guarded = ['id'];

    public function store()
    {
        return $this->belongsTo('App\Models\Store','store_id','id');
    }

    public function comment()
    {
        return $this->hasMany('App\Models\Comment');
    }
    public function proGet()
    {
//        $list = $this->where('store_id',session('storelist')['id'])->get();
        $list = $this->where('store_id',1)->get();
        if (empty($list)) {
            return null;
        }
        //dd($list);
        $data = [];
        $i = 0;
        foreach ($list as $k => $v) {
            //dd($v->name);
            $data[$i]['id'] = $v->id;
            $data[$i]['name'] = $v->name;
            $data[$i]['brand'] = $v->brand;
            $data[$i]['price'] = $v->price;
            $data[$i]['stock'] = $v->stock;
            $data[$i]['status'] = $v->status;
            $data[$i]['hod'] = $v->hod;
            $data[$i]['nature'] = $v->nature;
            $data[$i]['preview'] = $v->preview;
            $category = explode(',',$v->category_path);
            array_pop($category);
            $head = '';
            $foot = '';
            //dd($category);
            foreach ($category as $v1) {
                if (!empty(Category::find($v1))){
                    $name =Category::find($v1)->name;
                    $head .= "[{'text':'{$name}','nodes':";
                    $foot .= "}]";
                }else{
                    $head = "[{'text':'',";
                    $foot = "}]";
                }
            }
            $head= rtrim($head,"'nodes':");
            $str= rtrim($head,",");
            //dd($str);
            $str=$str.$foot;
            $data[$i]['category'] = $str;
            //类型遍历
            $data[$i]['type'] = rtrim($v->label,',');
 //           $type = explode(',',$v->label);
//            array_pop($type);
//            $tag = array_shift($type);
//            $head1 = "[{'text':'{$tag}','nodes':[{";
//            $foot1 =  "}]}]";;
//            //dd($category);
//            foreach ($type as $v2) {
//                $head1 .= "'text':'{$v2}',";
//            }
//            //dd($head);
//            $str1= rtrim($head1,",");
//
//            $str1=$str1.$foot1;
//            //dd($str1);
//            $data[$i]['type'] = $str1;

            $i++;

        }
        //dd($data);
        return $data;
    }

    public function proEdit($id)
    {
        $list = $this->find($id);

        $arr = explode(',',$list->label);
        array_pop($arr);
        $type = array_shift($arr);

        $list->label = $arr;

        $list->type = $type;

        $brr = explode(',',$list->category_path);
        array_pop($brr);
        foreach ($brr as $k=>$v){
            $name = Category::find($v)->name;

            $crr[$v] = $name;
        }
        $list->category_v = $crr;

        return $list;
    }

    public function photos($request)
    {
        if ($request->hasFile('file')) {

                //$file = $request->file('photos');
//                if($request->hasFile('images')){
//                    foreach($request->file('images') as $file) {
//                        $file->move(base_path().'/public/uploads/', $file->getClientOriginalName());
//                    }
//                }
                $imgs = [];
                $k = 0;
                foreach($request->file('file') as $file) {
                    $ext = $file->getClientOriginalExtension();
                    $picname = time().rand(1000,9999).'.'.$ext;
//                    $file->move('./upload/product/',$picname);
                    $img = Img::make($file);

                    $img->resize(620, null, function ($constraint) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    });
                    $img->save('./upload/product/'.$picname);
                    $imgs[$k] = $picname;
                    $k++;
                }

                if($file->getError()>0){
                    return null;
                }else{
                    return $imgs;

                }

        }

    }

    //查找单条药品信息
    public function selFirst($id)
    {
        $row = $this->where('id',$id)->first();
        if($row){
            return $row;
        }else{
            return null;
        }
    }
}
